/*
 * ESString.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

import org.scriptme.ecmascript.exceptions.EcmaScriptException;
import org.scriptme.ecmascript.interpreter.Evaluator;

/**
 * Implements the string primitive value.
 */
public final class ESString extends ESPrimitive {

	// The value
	/** The string. */
	private String string;

	/**
	 * Create a new value from the string parameters.
	 * 
	 * @param value
	 *            The immutable value
	 */
	public ESString(String value) {
		this.string = value;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toDetailString()
	 */
	public String toDetailString() {
		return "ES:\"" + string + "\"";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeOf()
	 */
	public int getTypeOf() {
		return EStypeString;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#getTypeofString()
	 */
	public String getTypeofString() {
		return "string";
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return string;
	}

	/**
	 * Returns the length of the string.
	 * 
	 * @return the length of the string
	 */
	public int getStringLength() {
		return string.length();
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#doubleValue()
	 */
	public double doubleValue() {
		double value = Double.NaN;
		try {
			// Will accept leading / trailing spaces, unlike new Integer !
			value = (Double.valueOf(string)).doubleValue();
		} catch (NumberFormatException e) {
		}
		return value;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#booleanValue()
	 */
	public boolean booleanValue() {
		return string.length() > 0;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toESString()
	 */
	public ESValue toESString() {
		return this;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toESObject(org.scriptme.ecmascript.interpreter.Evaluator)
	 */
	public ESValue toESObject(Evaluator evaluator) throws EcmaScriptException {
		StringPrototype theObject = null;
		ESObject sp = evaluator.getStringPrototype();
		theObject = new StringPrototype(sp, evaluator);
		theObject.value = this;
		return theObject;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#toJavaObject()
	 */
	public Object toJavaObject() {
		return string;
	}

	// overrides
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.scriptme.ecmascript.data.ESValue#isStringValue()
	 */
	public boolean isStringValue() {
		return true;
	}
}
